package cn.kgm.user.server.controller;

import cn.kgm.core.http.request.BaseController;
import cn.kgm.core.http.response.R;
import cn.kgm.user.dto.dto.UserLoginTypeSaveDTO;
import cn.kgm.user.dto.dto.UserLoginTypeUpdateDTO;
import cn.kgm.user.dto.entity.UserLoginType;
import cn.kgm.user.dto.query.UserLoginTypePageQuery;
import cn.kgm.user.dto.vo.UserLoginTypeVO;
import cn.kgm.user.server.service.UserLoginTypeService;
import cn.kgm.user.server.struct.UserLoginTypeMappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * # 技术改变自己 #
 * 用户可用登录类型表
 * @author Kappi
 * @date   2024-04-10 21:13:04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/userLoginType")
@Api(value = "UserLoginType", tags = "用户可用登录类型表")
public class UserLoginTypeController extends BaseController {

    @Autowired
    private UserLoginTypeService userLoginTypeService;

    /**
     * 分页列表查询用户可用登录类型表
     */
    @ApiOperation(value = "分页查询", notes = "分页查询用户可用登录类型表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "long", paramType = "query", defaultValue = "20"),
    })
    @GetMapping(value = "/page")
    public R<IPage<UserLoginTypeVO>> page(UserLoginTypePageQuery data) {
        IPage<UserLoginType> page = getPage();
        // 构建不为null的查询条件
        UserLoginType userLoginType =UserLoginTypeMappers.INSTANCE.mapToEntity(data);
        LambdaQueryWrapper<UserLoginType> wrapper = Wrappers.lambdaQuery(userLoginType);
        // 分页查询
            userLoginTypeService.page(page, wrapper);
        return R.success(page.convert((UserLoginTypeMappers.INSTANCE::mapToEntity)));
    }

    /**
     * 根据id查询用户可用登录类型表详情
     */
    @ApiOperation(value = "查看详情", notes = "根据id查询用户可用登录类型表详情")
    @GetMapping(value = "/{id}")
    public R<UserLoginTypeVO> get(@PathVariable(value = "id") Long id) {
        UserLoginType userLoginType =userLoginTypeService.getById(id);
        return R.success(UserLoginTypeMappers.INSTANCE.mapToEntity(userLoginType));
    }

    /**
     * 新增用户可用登录类型表
     */
    @ApiOperation(value = "新增", notes = "新增用户可用登录类型表")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody @Validated UserLoginTypeSaveDTO data) {
        UserLoginType userLoginType =UserLoginTypeMappers.INSTANCE.mapToEntity(data);
            userLoginTypeService.save(userLoginType);
        return R.success();
    }

    /**
     * 修改用户可用登录类型表
     */
    @ApiOperation(value = "修改", notes = "修改用户可用登录类型表")
    @PostMapping("/update")
    public R<Boolean> update(@RequestBody @Validated UserLoginTypeUpdateDTO data) {
        UserLoginType userLoginType =UserLoginTypeMappers.INSTANCE.mapToEntity(data);
        userLoginTypeService.updateById(userLoginType);
        return R.success();
    }

    /**
     * 根据id更新用户可用登录类型表的删除状态
     */
    @ApiOperation(value = "删除", notes = "根据id更新用户可用登录类型表的删除状态")
    @GetMapping(value = "/delete/{ids}")
    public R<Boolean> delete(@PathVariable(value = "ids") Long[] ids) {
            userLoginTypeService.update(Wrappers.<UserLoginType>lambdaUpdate()
                .set(UserLoginType::getIsDel, true)
                .in(UserLoginType::getId, ids));
        return R.success();
    }
}
